﻿using System;
using System.Collections.Generic;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Checker;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.DataStore;
using NTTDATA.BEENChina.ResultCertification.EntryModel;
using NTTDATA.BEENChina.ResultCertification.Logic;

namespace NTTDATA.BEENChina.ResultCertification.Result
{
    public class ChildImage  
    {
        private readonly object _lockobject = new object();

        public ChildImage()
        {
            OperatorEntryResults = new List<OpEntryResult>();
        }

        #region 子画像信息 来自store

        /// <summary>
        /// child image object 唯一标记 id
        /// 用于更新 检查结果
        /// </summary>
        public string SeqNo { get; set; }
        public string OrderId { get; set; }
        public string CategoryId { get; set; }
        public string FormId { get; set; }
        public string FormName { get; set; }
        public string CustomerId { get; set; }
        public string FieldId { get; set; }
        public string FieldName { get; set; }
        public string CategoryName { get; set; }
        
        //用于记录检查项目 记录到数据库
        private VerfityProperties _reason = new VerfityProperties();

        public VerfityProperties Reason
        {
            get { return _reason;}
            set { _reason = value;  }
        }

        public string ImageId { get; set; }

        //图片全路径名。包括文件名
        public string ImageFullPath { get; set; }

        //图像下载地址
        //from wcf
        public string ImageDownLoadAddress { get; set; }
        public string ImageDownLoadParam { get; set; }
        //画像图像
        public byte[] ImageData { get; set; }
        //画像扩展名
        public string ImageExtension{get; set; }

        /// <summary>
        /// OP的录入结果
        /// </summary>
        public List<OpEntryResult> OperatorEntryResults { get; set; }
        //录入规则
        //用于soho画面显示
        public string EntryRule { get; set; }

        #endregion


        #region verfity相关内容

        /// <summary>
        /// child image object 对应的category配置
        /// </summary>
        private CategoryConfig _categorySetting = null;

        public Config.CategoryConfig CategorySetting
        {
            get
            {
                if (_categorySetting == null)
                    _categorySetting = new CategoryConfigAccess().GetCagetoryConfig(CategoryId);

                return _categorySetting;
            }
            set { _categorySetting = value; } 
        }

        /// <summary>
        /// category对应的检查项目
        /// </summary>
        private IEnumerable<Verfity> _verfities = null;

        private  IEnumerable<Verfity> Verfities
        {
            get {

                lock (_lockobject)
                {
                    if (_verfities == null)
                    {
                        _verfities = new VerfityAccess().GetSelectedVerfitys(CategoryId);

                        //if (_verfities != null)
                        //    _verfities = _verfities.OrderBy(t => t.Id);
                    }

                    return _verfities;
                }
            }
            set { _verfities = value; }
        }
        

        public  bool Verfity()
        {
            if (Verfities == null)
                return false;

            bool verfityResult = false;
            int verfityWordCount = 0;

               foreach (var verfity in Verfities)
               {

                   try
                   {
                       if (verfity != null)
                       {
                          // ActionMessage.GetInstance().onProcessMessage( verfity.Id +" is Verfiting.... ", EnumProcessModel.取入数据); 
                           verfity.SetChildImage(this);
                           verfityResult = verfity.Check();
                           verfityWordCount = verfity.VerfityWordCount;
                           if (verfityResult)
                           {
                               //记住验证逻辑错误的ID号
                               Reason.VerfityItemId = verfity.Id;
                               break;
                           }
                       }
                   }
                   catch (Exception ex)
                   {

                       Log.Console.Error(
                           string.Format("ChildImage Verfity Error verfityid:{0} categoryid:{1}",
                           verfity.Id,CategoryId), ex);

                       throw  ;
                   }
                 

               }

            //每次子画像验证完毕
            //记录验证的字数
            //以后统计用
            /* WriteStatic(OrderId,CategorySetting.CategoryType,VerfityItemId, verfityWordCount); */

            //验证完毕 清空验证条件 减小内存

            Verfities = null;
            return verfityResult;
        }


        /// <summary>
        /// 存放确认结果
        /// </summary>
        public VerfityConfirmResult VerfityConfirmResult { get; set; }

        /// <summary>
        /// 临时存放store表名
        /// </summary>
        public string StoreTableName { get; set; }

        

        #endregion

        /// <summary>
        /// 每个子画像验证后 记录下验证的字数
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="categoryType"></param>
        /// <param name="verfityId"></param>
        /// <param name="resultLength"></param>
        protected void WriteStatic(string orderid, EnumCategoryType categoryType, string verfityId, int resultLength)
        {
            if (resultLength > 0)
            {
                ActionMessage.GetInstance().onProcessMessage(
                    string.Format("{0},{1},{2},{3},{4}", orderid,ImageId, categoryType, verfityId, resultLength),
                    EnumProcessModel.結果統計);
            }
        }

    }
}
